×
Boids Flocking Model - Complete Information
Overview
This is an implementation of Craig Reynolds' boids model (1986), which demonstrates emergent flocking behavior from simple local rules. The model simulates agent-based collective motion similar to bird flocks, fish schools, or insect swarms.
Mathematical Foundation
The model uses a discrete-time simulation where each agent (boid) updates its position and velocity at each timestep Δt.
Position Update: x(t+1) = x(t) + v(t) · Δt
Velocity Update: v(t+1) = v(t) + a(t) · Δt
where Δt = 1 (frame-based timestep)
Core Flocking Rules
1. Separation (Collision Avoidance)
Prevents crowding by steering away from nearby neighbors. The force increases with proximity.
F_sep = Σ[((p_self - p_neighbor) / |p_self - p_neighbor|²)] / n
Applied when: distance < separation_radius (25px default)
Force weighted by: 1/distance (inverse square law)
2. Alignment (Velocity Matching)
Steers toward the average heading of local flockmates to create coordinated movement.
v_desired = (Σ v_neighbor) / n
F_align = (v_desired · max_speed / |v_desired|) - v_self
3. Cohesion (Flock Centering)
Steers toward the average position of local flockmates to maintain group cohesion.
p_center = (Σ p_neighbor) / n
v_desired = p_center - p_self
F_cohesion = (v_desired · max_speed / |v_desired|) - v_self
Force Calculation and Limiting
All forces are calculated as steering forces (desired velocity - current velocity) and limited to prevent instantaneous direction changes:
a_total = w_sep·F_sep + w_align·F_align + w_coh·F_cohesion
If |a_total| > max_force: a_total = a_total · (max_force / |a_total|)
If |v| > max_speed: v = v · (max_speed / |v|)
Predator-Prey Dynamics
Predator Behavior
Target Selection:
Score = distance_to_boid - (5 - num_neighbors) · 30
Target = boid with minimum score
Predators preferentially hunt isolated boids (fewer neighbors within 50px).
Pursuit Strategy:
p_predicted = p_target + v_target · (distance / predator_speed)
v_desired = (p_predicted - p_predator) · predator_speed / |p_predicted - p_predator|
Uses predictive targeting (aims where prey will be, not where it is).
Parameters:
- Speed: 4.5 px/frame (150% of boid speed)
- Max Force: 0.03 (60% of boid agility)
- Catch Radius: 10px
- Detection Range: 300px
Boid Fear Response
urgency = 1 - (distance_to_predator / fear_radius)
panic_factor = urgency² · 10
F_flee = (p_boid - p_predator) · panic_factor / distance
max_panic_force = max_force · (1 + urgency · 4)
Fear response increases quadratically with proximity. When fleeing, normal flocking weights are reduced by (1 - 0.7 · flee_weight).
Initial Conditions
Boids:
- Position: Uniform random distribution across canvas
- Velocity: Random direction, magnitude = max_speed
- Initial heading: Random angle ∈ [0, 2π]
Predators:
- Position: Uniform random distribution
- Velocity: Random direction, magnitude = predator_speed
Respawning:
Caught boids respawn at random canvas edge with velocity directed inward.
Numerical Integration
The simulation uses Euler's method for integration with fixed timestep:
For each frame:
1. Calculate neighbors within perception_radius (O(n²) complexity)
2. Compute steering forces for each rule
3. Apply weights and sum forces
4. Limit acceleration to max_force
5. Update velocity: v += a
6. Limit velocity to max_speed
7. Update position: p += v
8. Handle boundary conditions (toroidal wrap)
Emergent Behaviors
- Murmuration: Wave-like patterns when flock responds to predators
- Selfish Herd: Increased density under predation pressure
- Fountain Effect: Flock splitting and flowing around threats
- Mill Formation: Circular motion when cohesion dominates
- Parallel Groups: Linear movement when alignment dominates
- Dynamic Clusters: Multiple sub-flocks when separation is high
Computational Complexity
Current implementation: O(n²) for neighbor detection per frame. Could be optimized to O(n log n) using spatial partitioning (quadtree or spatial hashing).
References
- Reynolds, C. W. (1987). "Flocks, herds and schools: A distributed behavioral model"
- Heppner, F. & Grenander, U. (1990). "A stochastic nonlinear model for coordinated bird flocks"
- Couzin, I. D. et al. (2002). "Collective memory and spatial sorting in animal groups"